package com.example.sherlockzhong.plush_v2.CardIni;

import android.os.Handler;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.sherlockzhong.plush_v2.CardBeans.AddOrgBean;
import com.example.sherlockzhong.plush_v2.Components.adapter.OrgListAdapter;
import com.example.sherlockzhong.plush_v2.Models.PushModel;
import com.example.sherlockzhong.plush_v2.R;
import com.example.sherlockzhong.plush_v2.Subscriptions.AddOrganizationActivity;
import com.example.sherlockzhong.plush_v2.Utils.DialogUtil;
import com.example.sherlockzhong.plush_v2.Utils.NetworkUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;


/*
 * Created by DosonLeung on 2014/8/25.
 */

public class AddOrganizationIni {
    public static OrgListAdapter init(final AddOrganizationActivity app, Handler handler,final JSONArray allPosterArray, AddOrgBean aob) {
        JSONObject allPosterObject;
        /*
        aob.setLength(3);
        aob.setImgs(new String[]{"","",""});
        aob.setTitles(new String[]{"人机交互工作室","人机交互工作室","人机交互工作室"});
        aob.setSubtitles(new String[]{"交流 开放 合作 创新","交流 开放 合作 创新","交流 开放 合作 创新"});
        */
        int length = allPosterArray.length();
        ArrayList<String> add_org_filepath = new ArrayList<String>();
        ArrayList<String> add_org_title = new ArrayList<String>();
        ArrayList<String> add_org_subtitle = new ArrayList<String>();
        ArrayList<String> add_org_id = new ArrayList<String>();
        JSONArray subscribedArray = PushModel.retrievePosters("subscribed");
        if (allPosterArray == null) {
            aob.setLength(0);
            aob.setFilepath(new String[]{});
            aob.setTitles(new String[]{});
            aob.setSubtitles(new String[]{});
        } else {
            try {
                int longs = length;
                for (int i = 0; i < length; i++) {
                    allPosterObject = allPosterArray.getJSONObject(i);
                    String id = allPosterObject.getString("id");
                    if (isContained(id, subscribedArray)) {
                        longs--;
                        continue;
                    }
                    add_org_filepath.add(allPosterObject.getString("logo_path"));
                    add_org_title.add(allPosterObject.getString("name"));
                    String introduction = allPosterObject.getString("introduction");
                    if(introduction.length()>33) {
                        introduction = introduction.substring(0, 30);
                        introduction+="...";
                    }
                    add_org_subtitle.add(introduction);
                    add_org_id.add(allPosterObject.getString("id"));
                }
                aob.setLength(longs);
                aob.setFilepath(add_org_filepath.toArray());
                aob.setTitles((add_org_title.toArray()));
                aob.setSubtitles((add_org_subtitle.toArray()));
                aob.setOrgId((add_org_id.toArray()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        OrgListAdapter SA = new OrgListAdapter(app, handler, aob.generateDataList(), R.layout.add_organization_item, aob.getItemName(), aob.getItemId(), aob.getOrgId());
        ListView add_org_list = (ListView) app.findViewById(R.id.add_organization_listview);
        add_org_list.setAdapter(SA);

//        add_org_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                try{
//                    DialogUtil.showDialogSingle(app,add_org_introduction_add.get((int)id));
//                }catch (Exception e){
//                    e.printStackTrace();
//                }finally {
//                    Toast.makeText(app.getBaseContext(),"查看失败",Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
        return SA;
    }

    private static boolean isContained(String s, JSONArray list) {
        int size = list.length();
        try {
            for (int i = 0; i < size; i++) {
                String test = list.getJSONObject(i).getString("id");
                if (test.equals(s))
                    return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void refreshEvent(ImageView refresh, AddOrganizationActivity app,boolean animation) {
        final AddOrganizationActivity apps = app;
        app.animation = animation;
        if(animation) {
            AnimationSet as = new AnimationSet(true);
            final float w = refresh.getWidth() / 2;
            final float h = refresh.getHeight() / 2;
            RotateAnimation ra = new RotateAnimation(0, 720, w, h);
            ra.setDuration(500);
            as.addAnimation(ra);
            refresh.startAnimation(as);
        }
        if (NetworkUtils.getAvailableNetWorkInfo(apps) != null) {
            if(animation)
                apps.showReflashDialog("加载中...");
            Thread load = apps.newLoadThread(apps.load);
            load.start();
        } else {
            Toast.makeText(apps, "刷新失败,请检查网络设置。", Toast.LENGTH_SHORT).show();
        }
    }

}
